package com.valentinalee.bms.web.servlet.filemanage3;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.valentinalee.bms.entity.FileSystemVO;
import com.valentinalee.bms.exception.ThisAppException;
import com.valentinalee.bms.service.FileSystemService;
import com.valentinalee.bms.service.impl.FileSystemServiceImpl;
import com.valentinalee.bms.util.ThisAppUtil;

public class FileManageServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//在这里写我们的业务代码
		try{
			req.setCharacterEncoding("utf-8");
			//分页大小
			String pageSizeStr=req.getParameter("pageSize");
			if(ThisAppUtil.isNone(pageSizeStr)){
				pageSizeStr="10";
			}
			int pageSize=Integer.parseInt(pageSizeStr);
			
			//当前页码
			String pageNoStr=req.getParameter("pageNo");
			if(ThisAppUtil.isNone(pageNoStr)){
				pageNoStr="1";
			}
			int pageNo=Integer.parseInt(pageNoStr);
			
			
			
			FileSystemService service=new FileSystemServiceImpl();
			List<FileSystemVO> pageData=new ArrayList<FileSystemVO>(pageSize);
			//查询文件信息等同于获取上传服务路径下的文件信息
			String key = "u";
			
			//调用下层的service
			int totalCount=service.queryFileSystemByKey(key, pageSize, pageNo, pageData);
			
			
			//返回
			req.setAttribute("key", key);
			req.setAttribute("pageSize", pageSize);
			req.setAttribute("currentPageNo", pageNo);
			req.setAttribute("pageData", pageData);
			int totalNo=totalCount/pageSize;
			if(totalCount%pageSize!=0){
				totalNo++;
			}
			
			req.setAttribute("totalNo", totalNo);
			
		}catch(ThisAppException e){
			req.setAttribute("message", e.getMessage());
		}catch(Exception e){
			req.setAttribute("message", "网络繁忙,请稍候再试");
			e.printStackTrace();
		}
		req.getRequestDispatcher("/jsp/file/queryFile.jsp").forward(req, resp);
		
		
	}
	
	

}
